﻿using GameFramework.Battle.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
using WorldMapBase;
using Yoozoo.HRP.Runtime;

namespace Yoozoo.Gameplay.RTS
{
#pragma warning disable 0429
    public class ModelInstancingGroup : MonoBehaviour
    {
        public ModelInstaning modelInstancing;
        public List<ModelGpuInstancing> modelGpuInstancingList = new List<ModelGpuInstancing>();
        public GPUSkinning_AllAnimation animationInfo;
        public List<int> specialPointsIndex = new List<int>();
        public List<Transform> specialPoints = new List<Transform>();
        private List<Transform> gunPoints;

        public void Initialize()
        {
            for(int i = 0; i < modelGpuInstancingList.Count; i++)
            {
                modelGpuInstancingList[i].SetConfig();
                modelGpuInstancingList[i].SetParent(modelInstancing.GetMountPointByIndex(modelGpuInstancingList[i].mountPointIndex));
                modelGpuInstancingList[i].SetShadowAlpha(180f / 255f);
            }
            for (int i = 0; i < specialPoints.Count; i++)
            {
                var gunPoint = specialPoints[i];
                var localPos = gunPoint.localPosition;
                var localRot = gunPoint.localRotation;
                var localScale = gunPoint.localScale;
                gunPoint.SetParent(modelInstancing.GetMountPointByIndex(specialPointsIndex[i]));
                gunPoint.localPosition = localPos;
                gunPoint.localRotation = localRot;
                gunPoint.localScale = localScale;
            }
        }

        public void Dispose()
        {
            for (int i = 0; i < modelGpuInstancingList.Count; i++)
            {
                var gpuInstance = modelGpuInstancingList[i];
                gpuInstance.OnRelease();
            }
        }
        
        public List<Transform> GetGunPoints()
        {
            if (gunPoints == null)
            {
                gunPoints = new List<Transform>();
                gunPoints.Add(transform);
                for (int i = 0; i < modelGpuInstancingList.Count; i++)
                {
                    gunPoints.Add(modelGpuInstancingList[i].gunPoint);
                }

                for (int i = 0; i < specialPoints.Count; i++)
                {
                    gunPoints.Add(specialPoints[i]);
                }
            }
            return gunPoints;
        }
    }
}
